//
//  GMInputViewEmojiView.m
//  Gmall
//
//  Created by yemeihua on 15/11/25.
//  Copyright © 2015年 Gmall. All rights reserved.
//

#import "GMInputViewEmojiView.h"

@interface GMInputEmotionView : UIButton

@property (nonatomic,readonly) UIImageView *iconView;
@property (nonatomic,readonly) UILabel *textLabel;
@property (nonatomic,copy) NSString *name;

@end

@implementation GMInputEmotionView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        _iconView = [[UIImageView alloc] init];
        //_iconView.userInteractionEnabled = YES;
        _iconView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:_iconView];
        
        _textLabel = [[UILabel alloc] init];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.textColor = [UIColor grayColor];
        _textLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_textLabel];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.and.height.equalTo(@60);
            make.top.equalTo(@0);
            make.centerX.equalTo(self);
        }];
        
        [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.mas_width);
            make.top.equalTo(self.iconView.mas_bottom).offset(8);
            make.left.equalTo(@0);
        }];
    }
    return self;
}

@end

@interface GMInputViewEmojiView()

@property (nonatomic,readonly) UIButton *defualtEmojiButton;
@property (nonatomic,readonly) UIButton *emojiButton1;
@property (nonatomic,readonly) UIButton *emojiButton2;
@property (nonatomic,readonly) UIButton *emojiButton3;
@property (nonatomic,readonly) BOOL enableGIF;

@end

@implementation GMInputViewEmojiView

- (instancetype)initWithFrame:(CGRect)frame enableGIF:(BOOL)enabled
{
    if(self = [super initWithFrame:frame])
    {
        _enableGIF = enabled;
        self.backgroundColor = [UIColor yc_colorWithUInt:0xf6f6f7];
        
        frame = self.bounds;
        frame.size.height -= 86.0;
        frame.origin.y = 16.0;
        
        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
        _scrollView.scrollEnabled = YES;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.yc_height - 70.0, kScreenWidth, 30)];
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = _emojis.count;
        [self addSubview:_pageControl];
        
        _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, self.yc_height - 40.0, kScreenWidth, 40.0)];
        _tabBarView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_tabBarView];
        
        UIImage *normalImg = [UIImage yc_imageWithColor:[UIColor whiteColor] size:CGSizeMake(64.0, 40.0)];
        UIImage *selectedImg = [UIImage yc_imageWithColor:[UIColor yc_colorWithUInt:0xf6f6f7] size:CGSizeMake(64.0, 40.0)];

        _defualtEmojiButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 40.0)];
        [_defualtEmojiButton setImage:[UIImage imageNamed:@"EmotionsEmojiHL"] forState:UIControlStateNormal];
        [_defualtEmojiButton setBackgroundImage:normalImg forState:UIControlStateNormal];
        [_defualtEmojiButton setBackgroundImage:selectedImg forState:UIControlStateSelected];
        [_defualtEmojiButton addTarget:self action:@selector(didTouchUpInsideEmotionButton:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarView addSubview:_defualtEmojiButton];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(64.0, 8, CGFloatFromPixel(1.0), 24.0)];
        lineView.backgroundColor = [UIColor yc_colorWithUInt:0xc4c4c4];
        [_tabBarView addSubview:lineView];
        
        if(self.enableGIF)
        {
            _emojiButton1 = [[UIButton alloc] initWithFrame:CGRectMake(65.0, 0, 64, 40.0)];
            [_emojiButton1 setImage:[UIImage imageNamed:@"Emotion1"] forState:UIControlStateNormal];
            [_emojiButton1 setBackgroundImage:normalImg forState:UIControlStateNormal];
            [_emojiButton1 setBackgroundImage:selectedImg forState:UIControlStateSelected];
            [_emojiButton1 addTarget:self action:@selector(didTouchUpInsideEmotionButton:) forControlEvents:UIControlEventTouchUpInside];
            [_tabBarView addSubview:_emojiButton1];
            
            lineView = [[UIView alloc] initWithFrame:CGRectMake(129.0, 8, CGFloatFromPixel(1.0), 24.0)];
            lineView.backgroundColor = [UIColor yc_colorWithUInt:0xc4c4c4];
            [_tabBarView addSubview:lineView];
            
            _emojiButton2 = [[UIButton alloc] initWithFrame:CGRectMake(130.0, 0, 64, 40.0)];
            [_emojiButton2 setImage:[UIImage imageNamed:@"Emotion2"] forState:UIControlStateNormal];
            [_emojiButton2 setBackgroundImage:normalImg forState:UIControlStateNormal];
            [_emojiButton2 setBackgroundImage:selectedImg forState:UIControlStateSelected];
            [_emojiButton2 addTarget:self action:@selector(didTouchUpInsideEmotionButton:) forControlEvents:UIControlEventTouchUpInside];
            [_tabBarView addSubview:_emojiButton2];
            
            lineView = [[UIView alloc] initWithFrame:CGRectMake(194.0, 8, CGFloatFromPixel(1.0), 24.0)];
            lineView.backgroundColor = [UIColor yc_colorWithUInt:0xc4c4c4];
            [_tabBarView addSubview:lineView];
            
            _emojiButton3 = [[UIButton alloc] initWithFrame:CGRectMake(195, 0, 64, 40.0)];
            [_emojiButton3 setImage:[UIImage imageNamed:@"Emotion3"] forState:UIControlStateNormal];
            [_emojiButton3 setBackgroundImage:normalImg forState:UIControlStateNormal];
            [_emojiButton3 setBackgroundImage:selectedImg forState:UIControlStateSelected];
            [_emojiButton3 addTarget:self action:@selector(didTouchUpInsideEmotionButton:) forControlEvents:UIControlEventTouchUpInside];
            [_tabBarView addSubview:_emojiButton3];
            
            lineView = [[UIView alloc] initWithFrame:CGRectMake(259.0, 8, CGFloatFromPixel(1.0), 24.0)];
            lineView.backgroundColor = [UIColor yc_colorWithUInt:0xc4c4c4];
            [_tabBarView addSubview:lineView];
            
            lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFloatFromPixel(1.0))];
            lineView.backgroundColor = [UIColor yc_colorWithUInt:0xc4c4c4];
            [_tabBarView addSubview:lineView];
        }
        
        self.type = GMInputViewEmotionTypeDefault;
    }
    return self;
}

- (void)didTouchUpInsideEmotionButton:(UIButton *)button
{
    if(!self.enableGIF)
    {
        return;
    }
    __block GMInputViewEmotionType type;
//    NSArray *buttons = @[self.defualtEmojiButton];
    NSArray *buttons = @[self.defualtEmojiButton,self.emojiButton1,self.emojiButton2,self.emojiButton3];
    [buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        if(obj == button)
        {
            type = idx;
        }
    }];
    button.selected = YES;
    self.type = type;
}

- (void)setType:(GMInputViewEmotionType)type
{
    _type = type;
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if(self.type == GMInputViewEmotionTypeDefault)
    {
        CGRect frame = CGRectMake(0, 0,self.scrollView.yc_width, self.scrollView.yc_height);
        __block NSInteger column = 0;
        
        [YCAdapter performOtherBlock:^{
            column = 7;
        } IPhone6Block:^{
            column = 8;
        } IPhone6pBlock:^{
            column = 9;
        }];
        
        NSArray *emojis = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"emoji" ofType:@"plist"]];
        NSInteger pageCount = 3 * column;
        _emojis = [[NSMutableArray alloc] init];
        
        NSMutableArray *pageEmojis;
        for(NSInteger i = 0;i < emojis.count;i++)
        {
            if(pageEmojis.count == pageCount || !pageEmojis)
            {
                pageEmojis = [[NSMutableArray alloc] init];
                [_emojis addObject:pageEmojis];
            }
            [pageEmojis addObject:emojis[i]];
        }
        
        _scrollView.contentSize = CGSizeMake(MAX(kScreenWidth,kScreenWidth * _emojis.count), _scrollView.yc_height);
        
        for(NSMutableArray *array in _emojis)
        {
            CGRect rect = CGRectInset(frame, 20, 0);
            UIView *view = [[UIView alloc] initWithFrame:rect];
            
            rect = CGRectMake(0, 0, (kScreenWidth - 40.0) / column, _scrollView.yc_height / 3.0);
            for(NSInteger i = 0;i < array.count;i++)
            {
                UIButton *button = [[UIButton alloc] initWithFrame:rect];
                [button setTitle:array[i] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:30];
                [button addTarget:self action:@selector(didTouchUpInsideEmojiButton:) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:button];
                
                rect = CGRectOffset(rect, CGRectGetWidth(rect), 0);
                if((i + 1) % column == 0 && i != 0)
                {
                    rect = CGRectMake(0, CGRectGetMaxY(rect), rect.size.width, rect.size.height);
                }
            }
            [_scrollView addSubview:view];
            
            frame = CGRectOffset(frame, CGRectGetWidth(frame), 0);
        }
        
        self.pageControl.numberOfPages = self.emojis.count;
    }
    else
    {
        CGRect frame = CGRectMake(0, 0,self.scrollView.yc_width, self.scrollView.yc_height);
        
        NSArray *emotionArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"emotions" ofType:@"plist"]];
        NSArray *emojis = emotionArray[self.type - 1];
        
        NSInteger column = 4;
        NSInteger pageCount = 2 * column;
        _emojis = [[NSMutableArray alloc] init];
        
        NSMutableArray *pageEmojis;
        for(NSInteger i = 0;i < emojis.count;i++)
        {
            if(pageEmojis.count == pageCount || !pageEmojis)
            {
                pageEmojis = [[NSMutableArray alloc] init];
                [_emojis addObject:pageEmojis];
            }
            [pageEmojis addObject:emojis[i]];
        }
        
        _scrollView.contentSize = CGSizeMake(MAX(kScreenWidth,kScreenWidth * _emojis.count), _scrollView.yc_height);
        for(NSMutableArray *array in _emojis)
        {
            CGRect rect = CGRectInset(frame, 20, 0);
            UIView *view = [[UIView alloc] initWithFrame:rect];
            
            rect = CGRectMake(0, 0, (kScreenWidth - 40.0) / column, _scrollView.yc_height / 2.0);
            for(NSInteger i = 0;i < array.count;i++)
            {
                NSDictionary *emo = array[i];
                
                GMInputEmotionView *button = [[GMInputEmotionView alloc] initWithFrame:rect];
                button.iconView.image = [UIImage imageNamed:emo[@"name"]];
                button.textLabel.text = emo[@"key"];
                button.name = emo[@"name"];
                [button addTarget:self action:@selector(didTouchUpInsideEmojiButton:) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:button];
                
                rect = CGRectOffset(rect, CGRectGetWidth(rect), 0);
                if((i + 1) % column == 0 && i != 0)
                {
                    rect = CGRectMake(0, CGRectGetMaxY(rect), rect.size.width, rect.size.height);
                }
            }
            [_scrollView addSubview:view];
            
            frame = CGRectOffset(frame, CGRectGetWidth(frame), 0);
        }
        
        self.pageControl.numberOfPages = self.emojis.count;

    }
    self.scrollView.contentOffset = CGPointZero;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger index = floor((scrollView.contentOffset.x - kScreenWidth/ 2) / kScreenWidth)+ 1;
    self.pageControl.currentPage = index;
}

- (void)didTouchUpInsideEmojiButton:(UIButton *)button
{
    if([self.delegate respondsToSelector:@selector(inputViewEmjojiView:didSelectText:type:title:)])
    {
        NSString *text = @"";
        NSString *title = @"";
        if([button isKindOfClass:([GMInputEmotionView class])])
        {
            GMInputEmotionView *b = (GMInputEmotionView *)button;
            text = [b name];
            title = b.textLabel.text;
        }
        else
        {
            text = [button titleForState:UIControlStateNormal];
        }
        [self.delegate inputViewEmjojiView:self didSelectText:text type:self.type title:title];
    }
}

@end
